package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad Profesor.<br>
 */
public class AccionesProfesor extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreBuscar_profesor.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreBuscar_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VBuscar_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FProfesorBuscar.clear(mapping, request);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreCrear_profesor.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FProfesor fFProfesor = FProfesor.clear(mapping, request);


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ABuscar_profesor.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ABuscar_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FProfesorBuscar fFProfesorBuscar = (FProfesorBuscar)form;
            request.getSession().setAttribute("ABuscar_profesor_campo", "ci");
            request.getSession().setAttribute("ABuscar_profesor_valor", "%" + fFProfesorBuscar.getCi() + "%");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreListar_profesor.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreListar_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VListar_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            String crit = (String)request.getSession().getAttribute("ABuscar_profesor_campo");
            java.util.List<Profesor> list;
            if (crit==null) {
                list = (java.util.List<Profesor>)s.createQuery("from Profesor").list();
            } else {
                String valor = (String)request.getSession().getAttribute("ABuscar_profesor_valor");
                list = (java.util.List<Profesor>)
                   s.createQuery("from Profesor where "+crit+" like :valor")
                    .setString("valor", valor).list();
                request.getSession().removeAttribute("ABuscar_profesor_campo");
                request.getSession().removeAttribute("ABuscar_profesor_valor");
            }
            request.setAttribute("L_Profesor", list);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_profesor.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idProfesor = lookup_Long(request, "idProfesor");
            
            FProfesor fFProfesor = FProfesor.clear(mapping, request);
            Profesor profesor = (Profesor)s.load(Profesor.class, idProfesor);
            fFProfesor.setIdProfesor(profesor.getIdProfesor());
            fFProfesor.setCi(profesor.getCi());
            fFProfesor.setPrimerNombre(profesor.getPrimerNombre());
            fFProfesor.setFec_Nac(profesor.getFec_Nac().toString());
            fFProfesor.setFec_Ing_USB(profesor.getFec_Ing_USB().toString());
            fFProfesor.setNovel(profesor.getNovel());
            fFProfesor.setPrimerApellido(profesor.getPrimerApellido());
            fFProfesor.setSegundoNombre(profesor.getSegundoNombre());
            fFProfesor.setSegundoApellido(profesor.getSegundoApellido());
            fFProfesor.setDivision(profesor.getDivision());
            fFProfesor.setDepartamento(profesor.getDepartamento());
            fFProfesor.setCategoria(profesor.getCategoria());
            fFProfesor.setCorreo(profesor.getCorreo());
            fFProfesor.setFec_Renuncia(profesor.getFec_Renuncia().toString());
            fFProfesor.setContratado(profesor.getContratado());

            for (Pai _Pai : profesor.getSet_Pai()) {
                Hibernate.initialize(_Pai.getProfesor());
            }
            request.setAttribute("L_Pai", profesor.getSet_Pai());
            for (Pas _Pas : profesor.getPas()) {
                Hibernate.initialize(_Pas.getProfesor());
            }
            request.setAttribute("L_Pas", profesor.getPas());
            for (Paa _Paa : profesor.getPaa()) {
                Hibernate.initialize(_Paa.getProfesor());
            }
            request.setAttribute("L_Paa", profesor.getPaa());

            request.getSession().setAttribute("idProfesor", idProfesor);
            request.getSession().setAttribute("idProfesor_super", idProfesor);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_profesor.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FProfesor fFProfesor = (FProfesor)form;
            Profesor profesor = (Profesor)s.load(Profesor.class, fFProfesor.getIdProfesor());
            profesor.setCi(fFProfesor.getCi());
            profesor.setPrimerNombre(fFProfesor.getPrimerNombre());
            profesor.setFec_Nac(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Nac()));
            profesor.setFec_Ing_USB(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Ing_USB()));
            profesor.setNovel(fFProfesor.getNovel());
            profesor.setPrimerApellido(fFProfesor.getPrimerApellido());
            profesor.setSegundoNombre(fFProfesor.getSegundoNombre());
            profesor.setSegundoApellido(fFProfesor.getSegundoApellido());
            profesor.setDivision(fFProfesor.getDivision());
            profesor.setDepartamento(fFProfesor.getDepartamento());
            profesor.setCategoria(fFProfesor.getCategoria());
            profesor.setCorreo(fFProfesor.getCorreo());
            profesor.setFec_Renuncia(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Renuncia()));
            profesor.setContratado(fFProfesor.getContratado());


            for (Pai _Pai : profesor.getSet_Pai()) {
                Hibernate.initialize(_Pai.getProfesor());
            }
            request.setAttribute("L_Pai", profesor.getSet_Pai());
            for (Pas _Pas : profesor.getPas()) {
                Hibernate.initialize(_Pas.getProfesor());
            }
            request.setAttribute("L_Pas", profesor.getPas());
            for (Paa _Paa : profesor.getPaa()) {
                Hibernate.initialize(_Paa.getProfesor());
            }
            request.setAttribute("L_Paa", profesor.getPaa());

            request.getSession().setAttribute("idProfesor", profesor.getIdProfesor());
            request.getSession().setAttribute("idProfesor_super", profesor.getIdProfesor());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_profesor.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_profesor.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_profesor", "V_profesor", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idProfesor = lookup_Long(request, "idProfesor");
            Profesor profesor = (Profesor)s.load(Profesor.class, idProfesor);
            s.delete(profesor);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_profesor.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_profesor.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_profesor.
     * Registro de la creación de una Profesor.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FProfesor fFProfesor = (FProfesor)form;
            Profesor profesor = new Profesor();
            profesor.setCi(fFProfesor.getCi());
            profesor.setPrimerNombre(fFProfesor.getPrimerNombre());
            profesor.setFec_Nac(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Nac()));
            profesor.setFec_Ing_USB(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Ing_USB()));
            profesor.setNovel(fFProfesor.getNovel());
            profesor.setPrimerApellido(fFProfesor.getPrimerApellido());
            profesor.setSegundoNombre(fFProfesor.getSegundoNombre());
            profesor.setSegundoApellido(fFProfesor.getSegundoApellido());
            profesor.setDivision(fFProfesor.getDivision());
            profesor.setDepartamento(fFProfesor.getDepartamento());
            profesor.setCategoria(fFProfesor.getCategoria());
            profesor.setCorreo(fFProfesor.getCorreo());
            profesor.setFec_Renuncia(new ve.usb.cohesion.runtime.LocalDate(fFProfesor.getFec_Renuncia()));
            profesor.setContratado(fFProfesor.getContratado());

            s.save(profesor);
            s.flush();
            fFProfesor.setIdProfesor(profesor.getIdProfesor());

            request.getSession().setAttribute("idProfesor", profesor.getIdProfesor());
            request.getSession().setAttribute("idProfesor_super", profesor.getIdProfesor());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_profesor.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }



}